export enum TipEnum {
    铁匠,
    木匠,
    剑客,
    恶魔,
    天使,
}

const quests: Quest[] = [{
    id: 1,
    name: '任务1',
}];

export const npcTips = [
    {
        tipID: TipEnum.天使,
        level: 1,
        tips: [
            {
                condition: (args) => true,
                tipID: TipEnum.剑客,
                level: 2,
            },
            {
                condition: (args) => true,
                tipID: TipEnum.木匠,
                level: 1,
            },
        ],
        quest: [
            {
                condition: (args) => true,
                questIDs: [1],
            },
        ],
        ai: [
            {
                condition: (args) => true,
                callable: () => {
                    console.log('天使ai');
                },
            },
        ],
        callable: (npc: NPC): NPC => {
            if (npc.tips.length >= 3) {
                npc.tips.push({
                    tipID: TipEnum.木匠,
                    level: 1,
                    exp: 0,
                    maxExp: 500,
                })
            }
            return npc;
        },
    },
    {
        tipID: TipEnum.剑客,
        level: 1,
        tips: [
            {
                condition: (args) => true,
                tipID: TipEnum.铁匠,
                level: 2,
            },
        ],
        quest: [
            {
                condition: (args) => true,
                questIDs: [1],
            },
        ],
        ai: [
            {
                condition: (args) => true,
                callable: () => {
                    console.log('剑客ai');
                },
            },
        ],
    }
];

export interface NPC {
    UUID: string;
    name: string;
    tips: {
        tipID: number;
        level: number;
        exp: number;
        maxExp: number;
    }[];
    quests: Quest[];
    ai: ((args: NPC) => void)[];
}

export interface Quest {
    id: number;
    name: string;
}

const generatorNPC = (tipID: TipEnum, deep: number = -1): NPC => {
    const levelExp = (level: number) => {
        return level * 100;
    }

    let npc: NPC = {
        UUID: generatorUUID(),
        name: '',
        tips: [{
            tipID,
            level: 1,
            exp: 0,
            maxExp: levelExp(1),
        }],
        quests: [],
        ai: [],
    }


    const addTipsRecursively = (currentTipID: TipEnum, currentDeep: number) => {
        if (currentDeep === 0) return;

        const currentTip = npcTips.find((item) => item.tipID === currentTipID);
        if (!currentTip) return;

        const availableTips = currentTip.tips.filter(tip => tip.condition(npc));
        if (availableTips.length === 0) return;

        const selectedTip = availableTips[Math.floor(Math.random() * availableTips.length)];

        npc.tips.push({
            tipID: selectedTip.tipID,
            level: selectedTip.level,
            exp: 0,
            maxExp: levelExp(selectedTip.level),
        });

        const nextDeep = currentDeep === -1 ? -1 : currentDeep - 1;
        if (nextDeep !== 0) {
            addTipsRecursively(selectedTip.tipID, nextDeep);
        }
    }

    addTipsRecursively(tipID, deep);

    const tip = npcTips.find((item) => item.tipID === tipID);

    for (const quest of tip.quest) {
        if (quest.condition(npc)) {
            npc.quests.push(...quest.questIDs.map((id) => quests.find((item) => item.id === id)));
        }
    }

    for (const ai of tip.ai) {
        if (ai.condition(npc)) {
            npc.ai.push(ai.callable);
        }
    }

    if (tip.callable) {
        npc = tip.callable(npc);
    }

    return npc;
}
const generatorUUID = () => {
    return Math.random().toString(36).substring(2).toString();
}

// console.log(generatorNPC(TipEnum.天使));


